import { Vec3 } from './types';

export enum GameMode {
  SURVIVAL = 'survival',
  CREATIVE = 'creative',
  HARDCORE = 'hardcore',
  SPECTATOR = 'spectator'
}

export interface PlayerCapabilities {
  canFly: boolean;
  invulnerable: boolean;
  infiniteResources: boolean;
  canInteract: boolean;
  canBreakBlocks: boolean;
  canPlaceBlocks: boolean;
  flySpeed: number;
  walkSpeed: number;
}

export interface GameModeConfig {
  mode: GameMode;
  capabilities: PlayerCapabilities;
  description: string;
}

export class GameModeManager {
  private currentMode: GameMode = GameMode.SURVIVAL;
  private onModeChangeCallback?: (mode: GameMode, capabilities: PlayerCapabilities) => void;
  private onPlayerDeathCallback?: () => void;

  // 预定义的游戏模式配置
  private modeConfigs: Map<GameMode, GameModeConfig> = new Map([
    [GameMode.SURVIVAL, {
      mode: GameMode.SURVIVAL,
      capabilities: {
        canFly: false,
        invulnerable: false,
        infiniteResources: false,
        canInteract: true,
        canBreakBlocks: true,
        canPlaceBlocks: true,
        flySpeed: 0,
        walkSpeed: 1.0
      },
      description: 'Survival mode: Gather resources, manage health and hunger, face challenges.'
    }],
    [GameMode.CREATIVE, {
      mode: GameMode.CREATIVE,
      capabilities: {
        canFly: true,
        invulnerable: true,
        infiniteResources: true,
        canInteract: true,
        canBreakBlocks: true,
        canPlaceBlocks: true,
        flySpeed: 2.0,
        walkSpeed: 1.5
      },
      description: 'Creative mode: Unlimited resources, flight, invulnerability for building.'
    }],
    [GameMode.HARDCORE, {
      mode: GameMode.HARDCORE,
      capabilities: {
        canFly: false,
        invulnerable: false,
        infiniteResources: false,
        canInteract: true,
        canBreakBlocks: true,
        canPlaceBlocks: true,
        flySpeed: 0,
        walkSpeed: 1.0
      },
      description: 'Hardcore mode: Like survival, but with permanent death.'
    }],
    [GameMode.SPECTATOR, {
      mode: GameMode.SPECTATOR,
      capabilities: {
        canFly: true,
        invulnerable: true,
        infiniteResources: false,
        canInteract: false,
        canBreakBlocks: false,
        canPlaceBlocks: false,
        flySpeed: 3.0,
        walkSpeed: 0
      },
      description: 'Spectator mode: Observe the world without interaction.'
    }]
  ]);

  constructor() {
    console.log('GameModeManager initialized with mode:', this.currentMode);
  }

  public switchMode(mode: GameMode): boolean {
    if (!this.modeConfigs.has(mode)) {
      console.error(`Invalid game mode: ${mode}`);
      return false;
    }

    const previousMode = this.currentMode;
    this.currentMode = mode;
    
    const config = this.modeConfigs.get(mode)!;
    
    console.log(`Game mode switched from ${previousMode} to ${mode}`);
    console.log(`Mode description: ${config.description}`);
    
    // 触发模式变更回调
    if (this.onModeChangeCallback) {
      this.onModeChangeCallback(mode, config.capabilities);
    }
    
    return true;
  }

  public getCurrentMode(): GameMode {
    return this.currentMode;
  }

  public getPlayerCapabilities(): PlayerCapabilities {
    const config = this.modeConfigs.get(this.currentMode);
    return config ? { ...config.capabilities } : this.getDefaultCapabilities();
  }

  public getModeConfig(mode: GameMode): GameModeConfig | undefined {
    return this.modeConfigs.get(mode);
  }

  public getAllModes(): GameMode[] {
    return Array.from(this.modeConfigs.keys());
  }

  public getModeDescription(mode: GameMode): string {
    const config = this.modeConfigs.get(mode);
    return config ? config.description : 'Unknown game mode';
  }

  public handlePlayerDeath(): void {
    console.log(`Player died in ${this.currentMode} mode`);
    
    if (this.currentMode === GameMode.HARDCORE) {
      // 硬核模式：永久死亡，切换到观察者模式
      console.log('Hardcore death detected - switching to spectator mode');
      this.switchMode(GameMode.SPECTATOR);
      
      // 触发死亡回调
      if (this.onPlayerDeathCallback) {
        this.onPlayerDeathCallback();
      }
    } else {
      // 其他模式：正常重生逻辑
      console.log('Player will respawn');
    }
  }

  public canRespawn(): boolean {
    return this.currentMode !== GameMode.HARDCORE;
  }

  public isCreativeMode(): boolean {
    return this.currentMode === GameMode.CREATIVE;
  }

  public isSurvivalMode(): boolean {
    return this.currentMode === GameMode.SURVIVAL;
  }

  public isHardcoreMode(): boolean {
    return this.currentMode === GameMode.HARDCORE;
  }

  public isSpectatorMode(): boolean {
    return this.currentMode === GameMode.SPECTATOR;
  }

  public setOnModeChangeCallback(callback: (mode: GameMode, capabilities: PlayerCapabilities) => void): void {
    this.onModeChangeCallback = callback;
  }

  public setOnPlayerDeathCallback(callback: () => void): void {
    this.onPlayerDeathCallback = callback;
  }

  private getDefaultCapabilities(): PlayerCapabilities {
    return {
      canFly: false,
      invulnerable: false,
      infiniteResources: false,
      canInteract: true,
      canBreakBlocks: true,
      canPlaceBlocks: true,
      flySpeed: 0,
      walkSpeed: 1.0
    };
  }

  // 调试和管理功能
  public getDebugInfo(): any {
    return {
      currentMode: this.currentMode,
      capabilities: this.getPlayerCapabilities(),
      availableModes: this.getAllModes(),
      canRespawn: this.canRespawn()
    };
  }

  // 序列化和反序列化（用于保存/加载）
  public serialize(): any {
    return {
      currentMode: this.currentMode
    };
  }

  public deserialize(data: any): void {
    if (data && data.currentMode && this.modeConfigs.has(data.currentMode)) {
      this.switchMode(data.currentMode);
    }
  }

  public handlePlayerRespawn(): void {
    console.log(`Player respawning in ${this.currentMode} mode`);
    
    // 在生存模式和创造模式下允许重生
    if (this.currentMode === GameMode.SURVIVAL || this.currentMode === GameMode.CREATIVE) {
      console.log('Player respawn allowed');
    } else if (this.currentMode === GameMode.HARDCORE) {
      console.log('Respawn not allowed in hardcore mode');
    }
  }
}